package com.labmi.controller.bean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import com.labmi.experiment.ClothingItem;
import com.labmi.experiment.Color;
import com.labmi.experiment.ItemStock;
import com.labmi.experiment.Size;
import com.labmi.experiment.dao.ItemDAO;
import com.labmi.utility.FacesUtils;

@ManagedBean(name = "itemDetailController")
@ViewScoped
public class ItemDetailController {

	@ManagedProperty(value="#{shop.lastVisitedItem}")
	private ClothingItem selectedItem;

	
	private String selectedColor;
	
	private String selectedSize;
	
	
	private Set<Size> distinctSizeSet; 
	
	
	public void setSelectedItem(ClothingItem selectedItem) {
		this.selectedItem = selectedItem;
	}

	public ClothingItem getSelectedItem() {
		return selectedItem;
	}


	public Set<String> getDistinctHexColorSet() {
		Set<String> colorSet = new HashSet<String>();
		
		if(selectedItem != null)
		{
			List<ItemStock> itemStockList = selectedItem.getItemStockList();
			for (ItemStock itemStock : itemStockList) {
					colorSet.add(itemStock.getColor().getHexCode());
			}
		}
		
		return colorSet;
	}

	/**
	 * 
	 * Return a distinct list of size available for some color.
	 * 
	 * E.g. An item with color black could contain XS L XL without replicating size (XS L L L XL). 
	 * 
	 * @return a Set containing all size available for some specific color. 
	 * 
	 */
	public Set<String> getAvailableSizeForThisColor() {
		
		Set<String> sizeSet = null;
		
		if(selectedColor != null)
		{
			sizeSet = new HashSet<String>();
			
			if(selectedItem != null)
			{
				List<ItemStock> itemStockList = selectedItem.getItemStockList();
				for (ItemStock itemStock : itemStockList) {
					if(itemStock.getColor().getHexCode().equals(selectedColor))	
						sizeSet.add(itemStock.getSize().getSizeName());
				}
			}
			
			
		}
		return sizeSet;
		
	}
	
	
	public boolean isDisabledButtonAdd()
	{
		if(selectedColor != null && selectedSize != null)
			return false;
		
		return true;

	}
	
	/**
	 * Increment the proprerty selectedQuantity inside the choosen ItemStock object
	 */
	public void updateItemStock(ActionEvent event) {
	    List<ItemStock> itemStockList = selectedItem.getItemStockList();
	    
	    for (ItemStock itemStock : itemStockList) {
			if( itemStock.getColor().getHexCode().equals(selectedColor) &&
				itemStock.getSize().getSizeName().equals(selectedSize)	)
			{	
				
				if(itemStock.getAvailableQuantity() > (itemStock.getSelectedQuantity() + 1))
				{
					itemStock.setSelectedQuantity( itemStock.getSelectedQuantity() + 1 );
				
					ItemDAO itemDAO = new ItemDAO();
				
					itemDAO.update(selectedItem);
				}
			}
			
			
		}
	    
	    selectedColor = null;
		selectedSize  = null;
	}

	
	
	public void setDistinctSizeSet(Set<Size> distinctSizeSet) {
		this.distinctSizeSet = distinctSizeSet;
	}

	public Set<Size> getDistinctSizeSet() {
		return distinctSizeSet;
	}

	public void setSelectedColor(String selectedColor) {
		
		String colorAsParam = FacesUtils.getHttpRequest().getParameter("param1");
		this.selectedColor = colorAsParam;
		
		this.selectedSize = null;
	}

	public String getSelectedColor() {
		return selectedColor;
	}

	public void setSelectedSize(String selectedSize) {
		this.selectedSize = selectedSize;
	}

	public String getSelectedSize() {
		return selectedSize;
	}
	
}
